package com.itheima.controller;

import com.itheima.common.CommonResult;
import com.itheima.common.PageInfo;
import com.itheima.pojo.Order;
import com.itheima.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @GetMapping("/page")
    public CommonResult page(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
                             @RequestParam(defaultValue = "") LocalDateTime beginTmie,
                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
                             @RequestParam(defaultValue = "") LocalDateTime endTime,
                             @RequestParam(defaultValue = "") String number,
                             @RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer pageSize) {

        PageInfo<Order> pageInfo = orderService.page(number, beginTmie, endTime, page, pageSize);

        return CommonResult.success(pageInfo);
    }

    @PutMapping
    public CommonResult updatestatus(@RequestParam("ids") String id, @RequestParam int status) {
        boolean res = orderService.updateStatus(id, status);
        if (!res) {
            return CommonResult.error("操作失败");
        }
        return CommonResult.success("操作成功");
    }

}
